package org.openfans.web.remote;

import org.openfans.domain.Facade;
import org.openfans.domain.Feed;
import org.openfans.feed.FeedManager;
import org.openfans.feed.FeedRetrieveException;
import org.openfans.util.ThreadFans;

public class FeedHandler {
	private Facade facade = null;

	private FeedManager feedManager;

	public void setFacade(Facade facade) {
		this.facade = facade;
	}

	public void setFeedManager(FeedManager feedManager) {
		this.feedManager = feedManager;
	}

	public String refreshFeed(int feedId) {
		Feed feed = facade.getFeed(feedId);
		try {
			feedManager.refreshFeed(feed);
		} catch (FeedRetrieveException e) {
			e.printStackTrace();
			return "error";
		}
		return "ok";
	}

	public String removeFeed(int feedId) {
		Feed feed = facade.getFeed(feedId);
		try {
			facade.remove(feed);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "ok";

	}
	
	public String subFeed(int feedId){
		Feed feed = facade.getFeed(feedId);
		try {
			facade.addFansSubFeed(facade.getFans(ThreadFans.get().getFansId()), feed);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "ok";
	}
	
	public String unsubFeed(int feedId){
		Feed feed = facade.getFeed(feedId);
		try {
			facade.removeFansSubFeed(facade.getFans(ThreadFans.get().getFansId()), feed);			
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "ok";
	}
	
	
	public String refreshAllFeed(){
		try {
			feedManager.refreshAllFeeds();
		} catch (Exception e) {
			return "error";
		}
		return "ok";
	}
}
